outputPath=getwd();

Scale = function(x, target_min, target_max, src_min=NA, src_max=NA){
	if (is.na(src_min)){
		src_min = min(x);
	}
	if (is.na(src_max)){
		src_max = max(x);
	}
	(target_max - target_min) / (src_max - src_min) * (x - src_min) + target_min;
}


sphere = function(x,y){
	x*x+y*y;
}

elliptic = function(x,y){
	C = 1e6;
	C^0 * x*x + C^1 * y *y;
}

rastrigin = function(x,y){
	a = x^2 - 10*cos(2*pi*x);
	b = y^2 - 10*cos(2*pi*y);
	10 + a + b;
}

ackley = function(x,y){
	a=x^2 + y^2;
	b=cos(2 * pi * x) + cos(2*pi*y);
	-20 * exp(-0.2 * sqrt(a/2)) - exp(b/2) + 20 + exp(1);
}

schwefel = function(x,y){
	x^2 + (x + y)^2;
}

rosenbrock = function(x,y){
	100 * (x^2 - y)^2 + (x-1)^2;
}


cols = colorRampPalette(c('black','white'))

Sphere.f = sphere;
Sphere.name = 'sphere';
Sphere.min = -5.12;
Sphere.max = 5.12;

Elliptic.f = elliptic;
Elliptic.name = 'elliptic';
Elliptic.min = -100;
Elliptic.max = 100;

Rastrigin.f = rastrigin;
Rastrigin.name = 'rastrigin';
Rastrigin.min = -5;
Rastrigin.max = 5;

Ackley.f = ackley;
Ackley.name = 'ackley';
Ackley.min = -32;
Ackley.max = 32;

Schwefel.f = schwefel;
Schwefel.name = 'schwefel';
Schwefel.min = -100;
Schwefel.max = 100;

Rosenbrock.f = rosenbrock;
Rosenbrock.name = 'rosenbrock';
Rosenbrock.min = -100;
Rosenbrock.max = 100;


benchmarks = c('Sphere', 'Elliptic', 'Rastrigin', 'Ackley', 'Schwefel', 'Rosenbrock');
#benchmarks = c('Elliptic');
for (f in benchmarks){
	message('Printing ', f);
	f.name = eval(parse(text=paste(f,'.name',sep='')));
	f.f = f.name;#parse(text=paste(f,'.f',sep=''));
	f.min = eval(parse(text=paste(f,'.min',sep='')));
	f.max = eval(parse(text=paste(f,'.max',sep='')));
	png(paste(outputPath,'/figure/f-', f.name, '.png',sep=''));
	par(mar=c(0,0,0,0));
	x = y = Scale(1:1000, f.min, f.max);
	f_xy = outer(x,y,f.f);
	image(x,y,f_xy, col=cols(255), axes=F, xlab=NA, ylab=NA,asp=1);
	dev.off();
}


